package com.spring.framework.model.xml.inject;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class LifecycleCallback {

    /**
     * 基于实现InitializingBean和DisposableBean接口的回调
     */
    public static class LifecycleCallbackImp implements InitializingBean, DisposableBean {
        public LifecycleCallbackImp() {
            System.out.println("LifecycleCallbackImp构造器调用");
        }

        @Override
        public void afterPropertiesSet() {
            System.out.println("LifecycleCallbackImp初始化回调");
        }

        @Override
        public void destroy() {
            System.out.println("LifecycleCallbackImp销毁回调");
        }

        @Override
        protected void finalize() {
            System.out.println("LifecycleCallbackImp销毁");
        }
    }

    /**
     * 基于XML的回调
     */
    public static class LifecycleCallbackXml {
        public LifecycleCallbackXml() {
            System.out.println("LifecycleCallbackXml构造器调用");
        }

        public void afterPropertiesSet() {
            System.out.println("LifecycleCallbackXml初始化回调");
        }

        public void destroy() {
            System.out.println("LifecycleCallbackXml销毁回调");
        }

        @Override
        protected void finalize() {
            System.out.println("LifecycleCallbackXml销毁");
        }
    }

    /**
     * 基于注解的回调
     */
    public static class LifecycleCallbackAnn {
        public LifecycleCallbackAnn() {
            System.out.println("LifecycleCallbackAnn构造器调用");
        }

        @PostConstruct
        public void afterPropertiesSet() {
            System.out.println("LifecycleCallbackAnn初始化回调");
        }

        @PreDestroy
        public void destroy() {
            System.out.println("LifecycleCallbackAnn销毁回调");
        }

        @Override
        protected void finalize() {
            System.out.println("LifecycleCallbackAnn销毁");
        }
    }

    /**
     * prototype回调测试
     */
    public static class LifecycleCallbackPro {
        public LifecycleCallbackPro() {
            System.out.println("prototype LifecycleCallbackPro构造器调用");
        }

        @PostConstruct
        public void afterPropertiesSet() {
            System.out.println("prototype LifecycleCallbackPro初始化回调");
        }

        @PreDestroy
        public void destroy() {
            System.out.println("prototype LifecycleCallbackPro销毁回调");
        }

        @Override
        protected void finalize() {
            System.out.println("prototype LifecycleCallbackPro销毁");
        }
    }
}
